import sqlite3
import tbitems
import tbitems_db_vip
# 打开数据库
def opendb():
    conn = sqlite3.connect("tb.db")
    cursor = conn.cursor()


# 关闭数据库
def closedb(conn,cursor):
    conn.commit()
    cursor.close()


#存入数据库
def main(itemid,page):
    # itemid = 588976817331
    # page = 2
    JSONlist=[]
    JSONlist=tbitems.tbmain(itemid, page)
    print(JSONlist)
    # 获取到list开始循环获得资源
    conn = sqlite3.connect("tb.db")
    cursor = conn.cursor()
    for i in JSONlist:
        if i['评论内容'] == '此用户没有填写评价。':
            continue
        try:
            tbitems_db_vip.initdb(cursor)
            tbitems_db_vip.insertdb(cursor,
                                    itemid,
                                    i['评论人'],
                                    i['评论头像'],
                                    i['评论时间'],
                                    i['评论内容'],
                                    i['购买商品类型'],
                                    str(i['评论视频截图']),
                                    str(i['评论视频']),
                                    str(i['好评图片']))
        except Exception as e:
            print(e)
            print('数据库插入错误')
            pass
    closedb(conn, cursor)

main(588976817331,1)

#读取数据库内容
def readinfo():
    text = []
    conn = sqlite3.connect("tb.db")
    cursor = conn.cursor()
    try:
        JSONlist = tbitems_db_vip.searchID(cursor, 588976817331)
        print(list(JSONlist[0]))
        for i in JSONlist:
            print(i[4])
            text.append(i[4])
    except Exception as e:
        print(e)
        #没有查到记录
        print("数据库没有该记录")
    print(text)
    closedb(conn, cursor)

# readinfo()